// components/clearInput/clearInput.js
var nameList = [];
var phone;
function nameBean() {
     var id;
     var name;
}
Component({
     externalClasses: ['input-class', 'icon-class'],

     /**
      * 组件的属性列表
      */
     properties: {
          inputHint: {
               type: String,
               value: '搜索'
          },
          inputIcon: {
               type: String,
               value: 'search.png'
          },
          inputType: {
               type: String,
               value: 'text'
          },
          isPassword: {
               type: Boolean,
               value: false
          },
          confirmType: {
               type: String,
               value: "done"
          },
          inputIndex:{
               type:String,
               value:'1'
          },
          inputValue:{
               type:String,
               value:''
          }
     },

     /**
      * 组件的初始数据
      */
     data: {
          isClearShow: false,
          inputValue: ''
     },

     /**
      * 组件的方法列表
      */
     methods: {
          inputListener: function (e) {
               //获取输入的居住人姓名和联系人电话
               var value = e.detail.value;
               var id=e.target.dataset.id;

               //控制清空按钮的显示隐藏
               if (value === null || value === undefined || value.length === 0) {
                    this.setData({
                         isClearShow: false
                    });
               } else {
                    this.setData({
                         isClearShow: true
                    });
               }

               //改变联系人电话和居住人姓名
               if(id!==null){
                    if(id=='phone'){
                         phone=value;
                    }else{
                         id=parseInt(id);
                         var flag=0;
                         var object=new nameBean();
                         object.id=id;
                         object.name=value;
                         for(var item in nameList){
                              //改变居住人的姓名
                              if(nameList[item].id==id){
                                   nameList[item].name=value;
                                   flag=1;
                              }
                         }
                         if(flag==0){nameList.push(object);}
                    }
               }

               //将数据传回页面
               var detail = {
                    nameList:nameList,
                    phone:phone
               };
               this.triggerEvent('inputListener', detail);
          },

          inputConfirm: function (e) {
               var value = e.detail.value;
               var detail = {
                    value: value
               }
               this.triggerEvent('inputConfirm', detail);
          },

          clearTap: function (e) {
               //获取清空的id
               var id=e.target.dataset.id;
               //清空联系人电话或居住人姓名
               if(id!=null){
                    if(id=='phone'){
                         phone='';
                    }else{
                         for(var item in nameList){
                              if(nameList[item].id==id){
                                   nameList[item].name='';
                              }
                         }
                    }
               }
               //将数据传回页面
               var detail = {
                    nameList:nameList,
                    phone:phone
               };
               this.triggerEvent('inputListener', detail);
               this.setData({
                    isClearShow: false,
                    inputValue: ''
               });
          }
     }
})
